package org.groupg.practice.configuration.validator;

import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.ex.ConfigurationException;

public class RegexValidator implements ConfigurationValidator {
    private final String key;
    private final String regex;

    public RegexValidator(String key, String regex) {
        this.key = key;
        this.regex = regex;
    }

    @Override
    public void validate(Configuration config) throws ConfigurationException {
        String value = config.getString(key);
        if (value == null || !value.matches(regex)) {
            throw new ConfigurationException("配置项 " + key + " 不符合规则: " + regex);
        }
    }
}